package com.legleg.util;

import com.jolbox.bonecp.BoneCPDataSource;
import com.legleg.conf.Configure;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-4-14
 * Time: 上午2:08
 * To change this template use File | Settings | File Templates.
 */
public class DBUtil {
    public static ThreadLocal<Connection> container = new ThreadLocal<Connection>();
    public static BoneCPDataSource ds = null;

    static {
        try {
            Class.forName(Configure.DB_CLASS);     // load the DB driver
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        ds = new BoneCPDataSource();  // create a new datasource object
        ds.setJdbcUrl(Configure.DB_URL);        // set the JDBC url
        ds.setUsername(Configure.DB_USERNAME);                // set the username
        ds.setPassword(Configure.DB_PASSWORD);                // set the password
        ds.setMaxConnectionsPerPartition(Configure.DB_POOL_MAXCONS);
        ds.setMinConnectionsPerPartition(Configure.DB_POOL_MINCONS);
        ds.setPartitionCount(Configure.DB_POOL_PARTITION_COUNT);
    }


    public static Connection getCon() {
        Connection con = null;
        try {
            con = ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void closeCon(Connection con) {
        if (con != null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*transaction method*/
    public static ThreadLocal<Connection> getContainer(){
        return container;
    }

    public static void startTransaction(){
        Connection con = container.get();
        if(con == null){
            con = getCon();
            container.set(con);
        }
        try{
            con.setAutoCommit(false);
        }catch (SQLException e){
            throw new RuntimeException(e.getMessage(),e);
        }
    }

    public static void commit() {
        Connection con = container.get();
        if(con != null){
            try {
                con.commit();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage(),e);
            }
        }
    }

    public static void rollback(){
        Connection con = container.get();
        if(con != null){
            try {
                con.rollback();
            } catch (SQLException e) {
                throw  new RuntimeException(e.getMessage(),e);
            }
        }
    }

    public static void close(){
        Connection con = container.get();
        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage(),e);
            } finally {
                container.remove();
            }
        }
    }


}
